<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>图书管理系统 - 登录注册</title>
    <style>
        body {
            font-family: '微软雅黑', sans-serif;
            background-color: #f5f6fa;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .btn {
            width: 100%;
            background-color: #3498db;
            color: white;
            padding: 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .btn:hover {
            background-color: #2980b9;
        }

        .register-link {
            text-align: center;
            margin-top: 15px;
        }

        .register-link a {
            color: #3498db;
            text-decoration: none;
        }

        .register-link a:hover {
            text-decoration: underline;
        }

        .register-container {
            display: none;
            background-color: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .register-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .close-btn {
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
            font-size: 20px;
            font-weight: bold;
        }

        .role-select {
            margin-bottom: 15px;
        }

        .role-select label {
            margin-right: 10px;
        }
    </style>
</head>
<body>

<!-- 登录容器 -->
<div class="login-container">
    <h2>登录</h2>
    <div class="role-select">
        <label><input type="radio" name="role" value="admin" checked> 管理员</label>
        <label><input type="radio" name="role" value="user"> 用户</label>
    </div>
    <form id="loginForm">
        <div class="form-group">
            <label for="loginUsername">用户名</label>
            <input type="text" id="loginUsername" required>
        </div>
        <div class="form-group">
            <label for="loginPassword">密码</label>
            <input type="password" id="loginPassword" required>
        </div>
        <button type="submit" class="btn">登录</button>
    </form>
    <div class="register-link">
        <a href="#" onclick="showRegisterForm(); return false;">没有账号？注册</a>
    </div>
</div>

<!-- 注册模态框 -->
<div class="register-container" id="registerContainer">
    <span class="close-btn" onclick="hideRegisterForm()">&times;</span>
    <h2>注册</h2>
    <form id="registerForm">
        <div class="form-group">
            <label for="registerUsername">用户名</label>
            <input type="text" id="registerUsername" required>
        </div>
        <div class="form-group">
            <label for="registerPassword">密码</label>
            <input type="password" id="registerPassword" required>
        </div>
        <div class="form-group">
            <label for="registerConfirmPassword">确认密码</label>
            <input type="password" id="registerConfirmPassword" required>
        </div>
        <div class="form-group">
            <label for="registerRealName">真实姓名</label>
            <input type="text" id="registerRealName" required>
        </div>
        <div class="form-group">
            <label for="registerContact">联系方式</label>
            <input type="text" id="registerContact" required>
        </div>
        <button type="submit" class="btn">注册</button>
    </form>
</div>

<script>
    // 显示注册表单
    function showRegisterForm() {
        document.getElementById('registerContainer').style.display = 'block';
    }

    // 隐藏注册表单
    function hideRegisterForm() {
        document.getElementById('registerContainer').style.display = 'none';
    }

    // 处理登录表单提交
    document.getElementById('loginForm').addEventListener('submit', async function (e) {
        e.preventDefault();
        const role = document.querySelector('input[name="role"]:checked').value;
        const username = document.getElementById('loginUsername').value;
        const password = document.getElementById('loginPassword').value;

        let url = '';
        let requestData = {};

        if (role === 'admin') {
            url = '/manager';
            requestData = { username, password };
        } else {
            url = '/user/login';
            requestData = { username, password };
        }

        try {
            const response = await fetch(url, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(requestData)
            });
            const data = await response.json();
            if (data.code === 1) {
                window.location.href = role === 'admin' ? 'admin.html' : 'user1.html';
            } else {
                alert('登录失败: ' + (data.msg || '请检查用户名和密码'));
            }
        } catch (error) {
            console.error('登录错误:', error);
            alert('网络错误，请稍后重试');
        }
    });

    // 处理注册表单提交
    document.getElementById('registerForm').addEventListener('submit', async function (e) {
        e.preventDefault();
        const username = document.getElementById('registerUsername').value.trim();
        const password = document.getElementById('registerPassword').value;
        const confirmPassword = document.getElementById('registerConfirmPassword').value;
        const realName = document.getElementById('registerRealName').value.trim();
        const phone = document.getElementById('registerContact').value.trim();

        if (!username || !password || !confirmPassword || !realName || !phone) {
            alert('请填写所有字段');
            return;
        }

        if (password !== confirmPassword) {
            alert('两次输入的密码不一致，请重新输入！');
            return;
        }

        // 检查用户名是否已被占用
        try {
            const checkRes = await fetch(`/user/exists?username=${encodeURIComponent(username)}`);
            const checkData = await checkRes.json();
            if (checkData.data) {
                alert('该用户名已被占用');
                return;
            }
        } catch (err) {
            alert('无法检查用户名，请稍后再试');
            return;
        }

        // 提交注册信息
        try {
            const res = await fetch('/user', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ username, password, realName, phone })
            });
            const data = await res.json();

            if (data.code === 1) {
                alert('注册成功！即将跳转登录页...');
                setTimeout(() => {
                    hideRegisterForm();
                    document.getElementById('registerForm').reset();
                    window.location.reload();
                }, 1000);
            } else {
                alert(data.msg || '注册失败，请稍后再试');
            }
        } catch (err) {
            alert('网络错误，请稍后重试');
        }
    });
</script>

</body>
</html>
